package com.zhoug.common3.adapters.recycler.dividers.sticky;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhoug.common3.utils.LogUtils;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 粘性标题适用于{@link androidx.recyclerview.widget.LinearLayoutManager} 布局
 * 支持自定义布局
 *  加载网络图片刷新问题还没解决
 * @Author 35574
 * @Date 2021/3/30
 * @Description
 */
public class ListStickyLayoutDecoration<T> extends ListStickyDecoration<T> {
    private static final String TAG = ">>>ListStickyLayoutDecoration";
    private static final boolean DEBUG = false;
    /**
     * 头部高度
     */
    private int mHeaderHeight = 0;
    /**
     * 头部View
     */
    private View mHeaderView;
    /**
     * 头部布局资源id
     */
    private @LayoutRes int headerLayoutId;
    /**
     * 绘制监听
     */
    private OnDrawListener<T> mOnDrawListener;

    public ListStickyLayoutDecoration( @LayoutRes int headerLayoutId) {
        this.headerLayoutId = headerLayoutId;
    }

    @Override
    protected int getHeaderHeight() {
        return mHeaderHeight;
    }

    @Override
    protected void onDrawHeader(Canvas canvas, Rect headerRect, GroupInfo<T> groupInfo) {
        int save = canvas.save();
        canvas.translate(headerRect.left, headerRect.top);
        if(mOnDrawListener!=null){
            mOnDrawListener.onBindHeader(mHeaderView,groupInfo);
        }
        mHeaderView.draw(canvas);
        canvas.restoreToCount(save);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        measureGroup(parent);
        super.getItemOffsets(outRect, view, parent, state);
    }

    /**
     * 测量头部
     * @param parent
     */
    private void measureGroup(RecyclerView parent) {
        if (mHeaderView == null) {
            mHeaderView = LayoutInflater.from(parent.getContext()).inflate(headerLayoutId, parent, false);
            if (mHeaderView.getLayoutParams() == null) {
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                mHeaderView.setLayoutParams(layoutParams);
            }
            int parentWidth = parent.getMeasuredWidth();
            int parentHeight = parent.getMeasuredHeight();
            if (DEBUG) {
                LogUtils.d(TAG, "measureGroup:parentWidth=" + parentWidth + ",parentHeight=" + parentHeight);
            }
            ViewGroup.LayoutParams layoutParams = mHeaderView.getLayoutParams();
            int widthSpec;
            int heightSpec;
            if (layoutParams.width > 0) {
                widthSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.width, View.MeasureSpec.EXACTLY);
            } else if (parentWidth > 0) {
                widthSpec = ViewGroup.getChildMeasureSpec(View.MeasureSpec.makeMeasureSpec(parentWidth, View.MeasureSpec.EXACTLY), parent.getPaddingStart() + parent.getPaddingEnd(), layoutParams.width);
            } else {
                widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            }

            if (layoutParams.height > 0) {
                heightSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.height, View.MeasureSpec.EXACTLY);
            } else if (parentHeight > 0) {
                heightSpec = ViewGroup.getChildMeasureSpec(View.MeasureSpec.makeMeasureSpec(parentHeight, View.MeasureSpec.EXACTLY), parent.getPaddingTop() + parent.getPaddingBottom(), layoutParams.height);
            } else {
                heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            }

            mHeaderView.measure(widthSpec, heightSpec);
            int headerWidth = mHeaderView.getMeasuredWidth();
            mHeaderHeight = mHeaderView.getMeasuredHeight();
            mHeaderView.layout(0, 0, headerWidth, mHeaderHeight);
            if (DEBUG) {
                LogUtils.d(TAG, "measureGroup:headerWidth=" + headerWidth+",headerHeight="+mHeaderHeight);
            }
        }

    }

    public void setOnDrawListener(OnDrawListener<T> onDrawListener) {
        this.mOnDrawListener = onDrawListener;
    }

    /**
     * 绘制
     * @param <T>
     */
    public interface OnDrawListener<T>{
        /**
         * 绑定头部信息
         * @param headerView
         * @param groupInf
         */
        void onBindHeader(View headerView,GroupInfo<T> groupInf);
    }


}
